package com.ruoyi.base.service.impl;

import java.util.List;

import com.ruoyi.base.domain.*;
import com.ruoyi.base.mapper.BaseFlowMapper;
import com.ruoyi.base.service.IBaseDeviceService;
import com.ruoyi.base.service.IBaseFlowTypeService;
import com.ruoyi.base.service.IBaseTypeService;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.base.mapper.BaseFlowIoMapper;
import com.ruoyi.base.service.IBaseFlowIoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 出入库Service业务层处理
 * 
 * @author yepanpan
 * @date 2025-04-15
 */
@Service
public class BaseFlowIoServiceImpl implements IBaseFlowIoService 
{
    private static final Logger log = LoggerFactory.getLogger(BaseFlowIoServiceImpl.class);

    @Autowired
    private BaseFlowIoMapper baseFlowIoMapper;
    @Autowired
    private BaseFlowMapper baseFlowMapper;
    @Autowired
    private IBaseFlowTypeService baseFlowTypeService;
    @Autowired
    private IBaseTypeService baseTypeService;
    @Autowired
    private IBaseDeviceService baseDeviceService;

    /**
     * 查询出入库
     * 
     * @param id 出入库主键
     * @return 出入库
     */
    @Override
    public BaseFlowIo selectBaseFlowIoById(Long id)
    {
        return baseFlowIoMapper.selectBaseFlowIoById(id);
    }

    /**
     * 查询出入库列表
     * 
     * @param baseFlowIo 出入库
     * @return 出入库
     */
    @Override
    @DataScope(deptAlias = "t", userAlias = "t")
    public List<BaseFlowIo> selectBaseFlowIoList(BaseFlowIo baseFlowIo)
    {
        return baseFlowIoMapper.selectBaseFlowIoList(baseFlowIo);
    }

    /**
     * 新增出入库
     * 
     * @param baseFlowIo 出入库
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBaseFlowIo(BaseFlowIo baseFlowIo)
    {
        BaseFlow baseFlow = baseFlowMapper.selectBaseFlowById(baseFlowIo.getFlowId());
        if(baseFlow == null){
            throw new ServiceException("工单不存在或者已完成");
        }
        if(!baseFlow.getFlowStatus().equals(BaseFlow.STATUS_DOING) && !baseFlow.getFlowStatus().equals(BaseFlow.STATUS_OUT)){
            throw new ServiceException("工单暂停或者已完成");
        }

        //采购入库
        int ret = 0;
        if(baseFlow.getFlowTypeCode().equalsIgnoreCase(BaseFlowType.TYPE_PURCHASE)){
            if(StringUtils.isEmpty(baseFlowIo.getDeviceCode())){
                throw new ServiceException("入库设备编码不能为空");
            }
            BaseType baseType = baseTypeService.selectBaseTypeById(baseFlow.getTypeId());
            BaseDevice baseDevice = new BaseDevice();
            baseDevice.setTypeId(baseFlow.getTypeId());
            baseDevice.setSysId(baseType.getSysId());
            baseDevice.setDeptId(baseFlow.getDeptId());
            baseDevice.setRunStatus(BaseDevice.RUN_NORMAL);
            baseDevice.setUseStatus(BaseDevice.USE_FREE);
            baseDevice.setCode(baseFlowIo.getDeviceCode());
            baseDevice.setPosition(baseFlowIo.getDeviceCode());
            ret =baseDeviceService.insertBaseDevice(baseDevice);
            baseFlowIo.setDeviceId(baseDevice.getId());

            BaseFlowIo fc = new BaseFlowIo();
            fc.setFlowId(baseFlowIo.getFlowId());
            List<BaseFlowIo> list = baseFlowIoMapper.selectBaseFlowIoList(fc);
            //判断是否完成
            if(list.size()+1 >= baseFlow.getAmount()){
                baseFlow.setFlowStatus(BaseFlow.STATUS_COMPLETE);
                ret += baseFlowMapper.updateBaseFlow(baseFlow);
            }
        }else if(baseFlowIo.getType().equalsIgnoreCase(BaseFlowIo.TYPE_IN)){
            //归还入库
            baseFlowIo.setDeviceId(baseFlow.getDeviceId());
            baseFlowIo.setDeviceCode(baseFlow.getDeviceCode());
            //流程结束
            baseFlow.setFlowStatus(BaseFlow.STATUS_COMPLETE);
            ret = baseFlowMapper.updateBaseFlow(baseFlow);
            //更新设备为闲置
            BaseDevice baseDevice = baseDeviceService.selectBaseDeviceById(baseFlow.getDeviceId());
            baseDevice.setUseStatus(BaseDevice.USE_FREE);
            baseDeviceService.updateBaseDeviceStatus(baseDevice);
        }else if(baseFlowIo.getType().equalsIgnoreCase(BaseFlowIo.TYPE_OUT)){
            //出库
            baseFlowIo.setDeviceId(baseFlow.getDeviceId());
            baseFlowIo.setDeviceCode(baseFlow.getDeviceCode());
            BaseFlowType baseFlowType = baseFlowTypeService.selectBaseFlowTypeById(baseFlow.getFlowTypeId());
            if(baseFlowType.getNeedRevert() != null && baseFlowType.getNeedRevert().equalsIgnoreCase("N")){
                //不需要归还则流程结束
                baseFlow.setFlowStatus(BaseFlow.STATUS_COMPLETE);
            }else{
                baseFlow.setFlowStatus(BaseFlow.STATUS_OUT);
            }
            ret = baseFlowMapper.updateBaseFlow(baseFlow);
            //更新设备为在用
            BaseDevice baseDevice = baseDeviceService.selectBaseDeviceById(baseFlow.getDeviceId());
            baseDevice.setUseStatus(BaseDevice.USE_NORMAL);
            baseDeviceService.updateBaseDeviceStatus(baseDevice);
        }else{
            throw new ServiceException("出入库类型不能为空");
        }

        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        baseFlowIo.setUserId(sysUser.getUserId());
        baseFlowIo.setDeptId(sysUser.getDeptId());
        baseFlowIo.setCreateBy(sysUser.getCreateBy());
        baseFlowIo.setCreateTime(DateUtils.getNowDate());
        ret += baseFlowIoMapper.insertBaseFlowIo(baseFlowIo);
        return ret;
    }

    /**
     * 修改出入库
     * 
     * @param baseFlowIo 出入库
     * @return 结果
     */
    @Override
    public int updateBaseFlowIo(BaseFlowIo baseFlowIo)
    {
        baseFlowIo.setUpdateTime(DateUtils.getNowDate());
        return baseFlowIoMapper.updateBaseFlowIo(baseFlowIo);
    }

    /**
     * 批量删除出入库
     * 
     * @param ids 需要删除的出入库主键
     * @return 结果
     */
    @Override
    public int deleteBaseFlowIoByIds(Long[] ids)
    {
        return baseFlowIoMapper.deleteBaseFlowIoByIds(ids);
    }

    /**
     * 删除出入库信息
     * 
     * @param id 出入库主键
     * @return 结果
     */
    @Override
    public int deleteBaseFlowIoById(Long id)
    {
        return baseFlowIoMapper.deleteBaseFlowIoById(id);
    }


}
